<style scoped>
.page{width: 100%;height: 100%;padding: 20px;flex-direction: column;}
.page-head{width: 100%;flex-wrap: wrap;justify-content: space-between;}
.page-head h4{line-height: 1.2;border-left: 4px solid var(--el-color-primary);padding-left: 1em;}
.buttons{justify-content: flex-end;flex-grow: 1;}
.tabs{width: 100%;}
.list{width: 100%;height: 100%;}
</style>
<template>
  <div class="page flex">
    <div class="page-head flex">
      <h4>{{config.title}}</h4>

      <div class="buttons flex">
        <el-button v-for="row in this_btns" :type="row.type" :size="row.size" @click="row.onclick">{{ row.title }}</el-button>
      </div>
    </div>
    <el-tabs v-model="channelNow" class="tabs max">
      <el-tab-pane v-for="(actionList, channel) in channelMap" :label="channelTitleMap[channel] || '未知'" :name="channel">
        <list :cols="list_cols" :data="actionList" class="list" :has_select="true" @select:list="onSelect">
          <template #action="{row}">
            <el-button v-if="can_edit" @click="to_edit(row)" size="small" type="primary">编辑</el-button>
            <el-button v-else @click="to_edit(row)" size="small">详情</el-button>
            <el-button v-if="row.tenant_id>0&&can_delete" @click="reset_row(row)" size="small" type="danger">恢复默认</el-button>
          </template>
        </list>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import {_config, decode_btns, decode_config, s94_event, to_string} from "@/js/s94_tool.js";
import {alert, confirm, error_back, iframe, in_right_admin, msg} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {S94Model} from "@/js/S94Model.js";
import forms from "@/components/list_url.vue";
import {use_config_map} from "@/store/config_map.js";

export default {
  components: {forms},
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model_name = this.$get('model_name', '');
    let model = S94Model.find(model_name);
    if (!model) error_back(`模型不存在：model_name=${model_name}`);

    if(in_right_admin('ModelV2Action/add')) btns['添加'] = function(){ iframe('model_v2_action/edit?'+app.param()) }
    if(in_right_admin('ModelV2Action/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let forms_config = {};

    let status_config = decode_config(model.status_alias_map.status);
    let pay_status_config = decode_config(model.status_alias_map.pay_status);

    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: ''},
      action:{name:'action', label:'操作名',width:'120px',align:'left'},
      label:{name:'label', label:'别名', width:'100px'},
      type:{name:'type', label:'操作类型',type:'select_one',config:[],width:'100px'},
      // operator:{name:'operator', label:'操作者', width:'100px'},

      tips:{name:'tips', label:'介绍', min_width:'120px'},

      need_status:{name:'need_status', label:'状态需求', type:'select_more', config:status_config, width:'140px'},
      need_pay_status:{name:'need_pay_status', label:'支付状态需求', type:'select_more', config:pay_status_config, width:'140px'},

      change_status:{name:'change_status', label:'修改状态', type:'select_one', config:status_config, width:'100px'},
      change_pay_status:{name:'change_pay_status', label:'修改支付状态', type:'select_one', config:pay_status_config, width:'100px'},

      // form_param_ids:{name:'form_param_ids', label:'参数ID集', min_width:'200px'},
      execute_ids:{name:'execute_ids', label:'执行ID集', width:'80px'},

      need_log:{label:'记录操作', name:'need_log', type:'radio_switch', value:0, width:'60px'},
      is_ban:{label:'是否禁用', name:'is_ban', type:'radio_switch', config:'禁用|1,启用|0', value:0, width:'60px'},
    };
    list_cols['_action_'] = {name:'', label:'操作', width:'160px', type:'action', fixed:'right'};

    return {
      can_edit: in_right_admin('ModelV2Action/edit'),
      can_delete: in_right_admin('ModelV2Action/delete'),
      model_name: model_name,
      list_cols: list_cols,
      channelNow: '',
      channelMap: {},
      channelTitleMap: {home:'用户端', admin:'管理员', system:'系统'},
      this_btns: decode_btns(btns),
      config: {
        api: '/admin/model_v2_action/list?model_name='+model_name,
        title: `${model.label}操作列表${is_del?'(已删除)':''}`,
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
      },
      selectIds: [],
    }
  },
  methods: {
    onSelect(selectList){
      this.selectIds = selectList.map(v=>v.id);
    },
    toDelete(){
      if (!this.selectIds.length) return msg.fail('所选内容为空');
      let message = `是否确定彻底删除选中的数据`;
      let ids = this.selectIds.join(',');
      let url = `/admin/model_v2_action/delete?model_name=${this.model_name}&ids=${ids}`
      confirm(message).then(()=>{
        ajax({url:url, data:'', loading:true},(res)=>{
          this.getList();
        })
      }).catch(()=>{})
    },
    getList(){
      ajax({
        url:`/admin/model_v2_action/list?model_name=${this.model_name}`,
        loading: true,
      }, (res)=>{
        let can_edit = in_right_admin('ModelV2Action/edit');
        let list = res.data.list;
        this.channelMap = {};
        list.forEach(row=>{
          let channel = row.channel;
          let channelRow = this.channelMap[channel] || []
          channelRow.push(row);
          this.channelMap[channel] = channelRow;
        })
        if (!this.channelMap[this.channelNow]){
          this.channelNow = Object.keys(this.channelMap)[0] || '';
        }
      });
    },
    param(data){
      data = data || {};
      data.model_name = this.model_name;
      return s94Mjs.param_encode(data);
    },
    to_edit(row){
      let param = {
        id: row.id,
      }
      if (!this.can_edit) param['is_info'] = 1;
      iframe('model_v2_action/edit?'+this.param(param))
    },
    async edit(row,col,v){
      if(!this.can_edit) return false;
      let res = await ajax({url: `/admin/model_v2_action/edit?id=${row.id}&${col.name}=${v}`, loading:true});
      if (res.data === 0) return false;
    },
    async reset_row(row){
      await confirm(`是否把操作配置 ${row.label} 恢复默认？`);
      let res = await ajax({url: `/admin/model_v2_action/delete?ids=${row.id}`, loading:true});
      if (res.data > 0) {
        this.getList();
      }
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (!this.model_name) {
      if (this.__iframe){
        iframe.remove('model_v2_action/index');
      }else {
        this.$router.back();
      }
      return;
    }

    this.getList();
    ajax({url:'/admin/model_v2_action/config_map', cache:true},(res)=>{
      this.list_cols.type.config = res.data.type_map;
      return false
    })
  }
}
</script>
